const fs = require('fs');
const path = require('path');

// 读取文件内容
const filePath = path.join(__dirname, 'app/controller/procurement/supplier_controller.js');
const content = fs.readFileSync(filePath, 'utf8');

// 尝试解析文件
try {
  // 移除注释以简化检查
  const cleanContent = content.replace(/\/\/.*$/gm, '').replace(/\/\*[\s\S]*?\*\//g, '');
  
  // 检查大括号匹配
  let braceCount = 0;
  for (let i = 0; i < cleanContent.length; i++) {
    if (cleanContent[i] === '{') {
      braceCount++;
    } else if (cleanContent[i] === '}') {
      braceCount--;
      if (braceCount < 0) {
        console.log('发现不匹配的闭合大括号在位置:', i);
        // 打印周围的代码
        const start = Math.max(0, i - 50);
        const end = Math.min(cleanContent.length, i + 50);
        console.log('周围代码:', cleanContent.substring(start, end));
        break;
      }
    }
  }
  
  console.log('大括号计数:', braceCount);
  
  // 检查是否有未闭合的try/catch块
  const tryCount = (cleanContent.match(/try/g) || []).length;
  const catchCount = (cleanContent.match(/catch/g) || []).length;
  
  console.log('try语句数量:', tryCount);
  console.log('catch语句数量:', catchCount);
  
  if (tryCount !== catchCount) {
    console.log('发现不匹配的try/catch块');
  } else {
    console.log('try/catch块匹配正确');
  }
  
  // 检查函数定义
  const asyncFunctionCount = (cleanContent.match(/async\s+\w+\s*\(/g) || []).length;
  const functionEndCount = (cleanContent.match(/}\s*$/gm) || []).length;
  
  console.log('async函数数量:', asyncFunctionCount);
  console.log('函数结束数量:', functionEndCount);
  
} catch (error) {
  console.error('语法检查失败:', error.message);
}